package com.vision.world.survey.fragments;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.Switch;

import com.vision.world.survey.R;
import com.vision.world.survey.presenter.SettingLayerPresenter;
import com.vision.world.survey.Interface.view.ISettingLayerView;
import com.vision.world.survey.util.CommonUtil;

public class SettingLayerFragment extends Fragment implements ISettingLayerView,
        View.OnTouchListener, View.OnClickListener,
        CompoundButton.OnCheckedChangeListener{
    private Context mContext;
    private GridView mGridView;
    private LinearLayout touchPanel;
    private LinearLayout disSettingPanel;
    private ConstraintLayout clSettingPanel;
    private Switch aSwitch;
    private SettingLayerPresenter mSettingLayerPresenter;

    private OnSettingLayerFragmentListener mListener;

    public SettingLayerFragment(){}

    @SuppressLint({"NewApi", "ValidFragment"})
    public SettingLayerFragment(Context context) {
        // Required empty public constructor
        mContext=context;
    }

    public void setListener(OnSettingLayerFragmentListener listener) {
        mListener = listener;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_layer_setting, container, false);
        initData();
        mGridView=(GridView)view.findViewById(R.id.gv_layers);
        mGridView.setAdapter(mSettingLayerPresenter.getLayerSettingAdapter());
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                mSettingLayerPresenter.changeBaseLayerDisplay(position);
            }
        });
        touchPanel = (LinearLayout) view.findViewById(R.id.ll_setting_touch_dis);
        touchPanel.setOnTouchListener(this);
        clSettingPanel=(ConstraintLayout)view.findViewById(R.id.cl_setting_panel);
        clSettingPanel.setOnTouchListener(this);
        disSettingPanel=(LinearLayout)view.findViewById(R.id.ll_search_dis_setting);
        disSettingPanel.setOnClickListener(this);
        aSwitch = (Switch) view.findViewById(R.id.switch_is_fresh_search);
        aSwitch.setOnCheckedChangeListener(this);
        aSwitch.setChecked(CommonUtil.isFreshIdentify);
        return view;
    }

    public void initData(){
        mSettingLayerPresenter=new SettingLayerPresenter(mContext,this);
    }

    @Override
    public void changeLayer(String url) {
        mListener.OnChangeLayer(url);
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (view.getId()){
            case R.id.cl_setting_panel:

                return true;
            case R.id.ll_setting_touch_dis:
                mListener.OnDismissPanel();
                return true;
        }
        return false;
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.ll_search_dis_setting:
                settingDis();
                break;
        }
    }

    public void settingDis(){
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
//        builder.setIcon(android.R.drawable.ic_dialog_info);
        builder.setTitle("请输入搜索距离");
        //把布局文件先填充成View对象
        View view = View.inflate(mContext, R.layout.dialog_setting_distance, null);
        //把填充得来的view对象设置为对话框显示内容
        builder.setView(view);
        final EditText editText=(EditText)view.findViewById(R.id.et_distance) ;
        editText.setText(""+CommonUtil.MapSelectDistance/1000);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String distance=editText.getText().toString();
                CommonUtil.MapSelectDistance=Double.parseDouble(distance)*1000;
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.show();
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        switch (compoundButton.getId()) {
            case R.id.switch_is_fresh_search:
                  CommonUtil.isFreshIdentify=b;
                break;
        }
    }

    public interface OnSettingLayerFragmentListener {
        void OnDismissPanel();
        void OnChangeLayer(String url);
    }
}
